package top.bulk.encrypt.strategy;

import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.AES;
import lombok.AllArgsConstructor;
import lombok.Getter;
import top.bulk.encrypt.util.CryptoUtils;

import static cn.hutool.crypto.asymmetric.KeyType.PrivateKey;
import static cn.hutool.crypto.asymmetric.KeyType.PublicKey;

/**
 * 加密策略
 *
 * @author 散装java
 * @date 2024-07-18
 */
@Getter
@AllArgsConstructor
public enum CryptoStrategy {

    /**
     * 使用 aes 加密
     */
    AES {
        @Override
        public String encrypt(CryptoWrapper cryptoWrapper) {
            String fieldValue = cryptoWrapper.getFieldValue();
            AES aes = CryptoUtils.aes(cryptoWrapper.getArguments());
            return aes.encryptHex(fieldValue);
        }

        @Override
        public String decrypt(CryptoWrapper cryptoWrapper) {
            String fieldValue = cryptoWrapper.getFieldValue();
            AES aes = CryptoUtils.aes(cryptoWrapper.getArguments());
            return aes.decryptStr(fieldValue);
        }
    },
    RSA {
        @Override
        public String encrypt(CryptoWrapper cryptoWrapper) {
            String fieldValue = cryptoWrapper.getFieldValue();
            String[] arguments = cryptoWrapper.getArguments();

            RSA rsa = CryptoUtils.rsa(arguments);
            // 公钥加密
            return rsa.encryptHex(fieldValue, PublicKey);
        }

        @Override
        public String decrypt(CryptoWrapper cryptoWrapper) {
            String fieldValue = cryptoWrapper.getFieldValue();
            String[] arguments = cryptoWrapper.getArguments();

            RSA rsa = CryptoUtils.rsa(arguments);
            // 私钥解密
            return rsa.decryptStr(fieldValue, PrivateKey);
        }
    },

    // 可自定拓展其他加密算法
    ;

    public abstract String encrypt(CryptoWrapper cryptoWrapper);


    public abstract String decrypt(CryptoWrapper cryptoWrapper);
}
